
#include "stdafx.h"
#include "HRKStream.h"

CHrkStream::CHrkStream()
{
	m_nTabs = 0;
}

CHrkStream::~CHrkStream()
{
}
	
void CHrkStream::SetTab( int nTabs )
{
	m_nTabs = nTabs;
}

void CHrkStream::ResetTab()
{
	m_nTabs = 0;
}

void CHrkStream::IncTab()
{
	m_nTabs++;
}

void CHrkStream::DecTab()
{
	m_nTabs--;
	if( m_nTabs< 0)
	{
		m_nTabs = 0;
	}
}


void CHrkStream::PrintString(  const TCHAR* fmt,...  )
{
	TCHAR* buff = NULL;

	int nChars = 0;

	va_list ap;
	va_start( ap,fmt);
	nChars = _vsctprintf( fmt,ap);
	va_end( ap );

	if( nChars )
	{
		buff = (TCHAR*)malloc( sizeof(TCHAR)*( nChars + 1) );
		
		va_start( ap,fmt );
		nChars = _vstprintf( buff,fmt,ap );
		va_end( ap );

		OutputString( buff );

		free(buff);
	}
}

void CHrkStream::PrintLine( const TCHAR* fmt,... )
{
	TCHAR* buff = NULL;

	int nChars = 0;

	va_list ap;
	va_start( ap,fmt);
	nChars = _vsctprintf( fmt,ap);
	va_end( ap );

	if( nChars )
	{
		buff = (TCHAR*)malloc( sizeof(TCHAR)*( nChars + 1) );
		
		va_start( ap,fmt );
		nChars = _vstprintf( buff,fmt,ap );
		va_end( ap );

		OutputString( buff );

		free(buff);
	}

	PrintEndLine();
}

void CHrkStream::PrintTabs()
{
	int nTabs = m_nTabs;

	if( nTabs < 0)
	{
		nTabs = 0;
	}

	if( nTabs )
	{
		for( int i = 0;i<m_nTabs;i++ )
		{
			OutputString("\t");

		}
	}
}

void CHrkStream::PrintEndLine()
{
	OutputString("\n");
}

void CHrkStream::OutputString( const TCHAR* str )
{
	if(is_open())
	{
		*this<<str;
	}
}

void CHrkStream::PrintTabString( const TCHAR* fmt,... )
{
	PrintTabs();
	
	TCHAR* buff = NULL;

	int nChars = 0;

	va_list ap;
	va_start( ap,fmt);
	nChars = _vsctprintf( fmt,ap);
	va_end( ap );

	if( nChars )
	{
		buff = (TCHAR*)malloc( sizeof(TCHAR)*( nChars + 1) );
		
		va_start( ap,fmt );
		nChars = _vstprintf( buff,fmt,ap );
		va_end( ap );

		OutputString( buff );

		free(buff);
	}
}

void CHrkStream::PrintTabLine( const TCHAR* fmt,... )
{
	PrintTabs();

	TCHAR* buff = NULL;

	int nChars = 0;

	va_list ap;
	va_start( ap,fmt);
	nChars = _vsctprintf( fmt,ap);
	va_end( ap );

	if( nChars )
	{
		buff = (TCHAR*)malloc( sizeof(TCHAR)*( nChars + 1) );

		va_start( ap,fmt );
		nChars = _vstprintf( buff,fmt,ap );
		va_end( ap );

		OutputString( buff );

		free( buff );
	}

	PrintEndLine();
}